package org.example.ychdp.controller;

import jakarta.servlet.http.HttpSession;
import org.example.ychdp.entity.Concert;
import org.example.ychdp.entity.User;
import org.example.ychdp.service.ConcertService;
import org.example.ychdp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Controller
public class loginController {

    @Autowired
    private ConcertService concertService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/hello")
    public ModelAndView login() {
        List<Concert> concertList = concertService.selectConcertToIndex();
        Map<String,Object> model = new HashMap<String,Object>();
        model.put("ConcertL",concertList);
        ModelAndView modelAndView = new ModelAndView("index",model);
        return modelAndView;
    }


    @RequestMapping(value = "/login")
    public String loginPost(Model model) {
//        // 从模型中获取errorMsg属性，如果存在则可以在视图中展示相应的提示信息
//        String errorMsg = (String) model.getAttribute("errorMsg");
//        // 这里可以将errorMsg再添加到模型中，方便在Thymeleaf模板等中使用，当然根据实际情况也可以有其他处理方式
//        model.addAttribute("errorMsg", errorMsg);
//        System.out.println(errorMsg);
        return "login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ModelAndView loginSuccess(@RequestParam String username,
                                     @RequestParam String password,
                                     HttpSession session,
                                     String captcha) {
        List<Concert> concertList = concertService.selectConcertToIndex();
        Map<String,Object> model = new HashMap<>();

        // 校验验证码
        String Captcha = (String) session.getAttribute("captcha");
        if (Captcha == null || !Captcha.equalsIgnoreCase(captcha)) {
            String errorMsg = "验证码错误";
            model.put("errorMsg",errorMsg);
            ModelAndView modelAndView = new ModelAndView("login",model);
            return modelAndView;
        }

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        User user1 = userService.selectUserByName(user);
        if (user1==null){
            String errorMsg = "账号不存在";
            model.put("errorMsg",errorMsg);
            ModelAndView modelAndView = new ModelAndView("login",model);
            return modelAndView;
        }
        if ((!Objects.equals(user1.getPassword(), user.getPassword()))){
            String errorMsg = "账号或密码错误";
            model.put("errorMsg",errorMsg);
            ModelAndView modelAndView = new ModelAndView("login",model);
            return modelAndView;
        }
        System.out.println(user);
        session.setAttribute("user",user1);
        System.out.println(userService.selectUserByName(user));

        model.put("ConcertL",concertList);
        ModelAndView modelAndView = new ModelAndView("index",model);
        return modelAndView;
    }

    @RequestMapping(value = "register")
    public String register() {
        return "register";
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String registerSuccess(User user, Model model) {
        int i = userService.insertUser(user);
        if (i < 1){
            String errorMsg = "注册失败，请重新注册";
            model.addAttribute("errorMsg",errorMsg);
            return "login";
        }
        return "login";
    }

    @RequestMapping(value = "/index")
    public ModelAndView index() {
        List<Concert> concertList = concertService.selectConcertToIndex();
        Map<String,Object> model = new HashMap<String,Object>();
        model.put("ConcertL",concertList);
        ModelAndView modelAndView = new ModelAndView("index",model);
        return modelAndView;
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "index";
    }
}
